package com.aqa.concurrent;

import org.junit.Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {


    @Test
    public void test() throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();

        Condition condition = lock.newCondition();
        Thread thread1 = new Thread(() -> {
            try {
                lock.lock();
                condition.await();
                System.out.println("-------- 线程1-等待队列① wake up----------");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "线程1-等待队列①");

        Thread thread2 = new Thread(() -> {
            try {
                lock.lock();
                condition.await();
                System.out.println("-------- 线程2-等待队列② wake up----------");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "线程2-等待队列②");

        Thread thread3 = new Thread(() -> {
            try {
                lock.lock();
                condition.signalAll();
                System.out.println("-------- 线程3-等待队列③ wake up----------");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "线程3-等待队列③");

        thread1.start();
        thread2.start();
        Thread.sleep(1000);
        thread3.start();
        Thread.sleep(1000);
        Thread.currentThread().join();

    }

}
